<?php
/**
 * 通过小程序提交的团检申请
 *
 * User: kaikaic
 * Date: 2018/11/24
 * Time: 12:22
 */

namespace app\exam\controller;

use app\exam\service\EnterpriseService;
use app\exam\service\GroupApplyService;
use app\exam\service\GroupService;
use app\exam\service\GroupSetService;
use app\exam\service\MecService;
use app\exam\service\MessageService;
use controller\BasicAdmin;
use service\ConfigService;
use think\Db;

class Apply extends BasicAdmin {

    protected $table = "ExamGroupApply";

    public function index() {
        $this->title = "体检申请";
        $query = array();
        $query['title'] = $this->request->get("title");

        $db = Db::name($this->table);
        if ($query['title']) {
            $db->whereLike('title', "%{$query['title']}%");
        }

        return parent::listRecord($db);
    }

    /**
     * 体检申请-审核
     */
    public function verify() {
        $id = intval($this->request->param('id'));
        if (empty($id)) {
            $this->error('参数错误，请稍候再试！');
        }

        $record = GroupApplyService::getInstance()->find($id);
        if (empty($record)) {
            $this->error('记录不存在，请重新操作!');
        }
        if ($record['status'] != 1) {
            $this->error('状态错误，申请已审核！');
        }

        if ($this->request->isGet()) {
            return view('form', [
                'title'=>'申请审核',
                'vo'=>$record,
            ]);
        }

        if ($this->request->isPost()) {
            $verify = intval($this->request->post("ok"));
            $comment = trim($this->request->post("comment"));

            $status = 0;
            if ($verify == 1) {
                $status = 2;
            }else if ($verify == 2) {
                $status = 3;
            }
            if (!$status) {
                $this->error('参数错误，请稍候再试！');
            }

            $update = array();
            $update['status'] = $status;
            $update['comment'] = $comment;
            $result = GroupApplyService::getInstance()->update($update, $id);
            if ($result) {
                if ($status == 2) {
                    $rs = GroupApplyService::getInstance()->initGroup($id);
                    if ($rs) {
                        // 发送消息
                        MessageService::getInstance()->sendApplyMessage();
                    }else {
                        $this->error('操作失败，请稍候再试！');
                    }
                }

                $this->success('操作完成！', '');
            }else {
                $this->error('操作失败，请稍候再试！');
            }
        }
    }

    /**
     * 体检申请-查看
     */
    public function view() {
        $id = intval($this->request->param('id'));
        if (empty($id)) {
            $this->error('参数错误，请稍候再试！');
        }

        $record = GroupApplyService::getInstance()->find($id);
        if (empty($record)) {
            $this->error('记录不存在，请重新操作!');
        }

        return view('view', [
            'title'=>'查看申请',
            'vo'=>$record,
        ]);
    }

    /**
     * 体检申请-删除
     */
    public function remove() {
        $id = intval($this->request->post('id', ''));
        if (empty($id)) {
            $this->error('参数错误!');
        }

        $record = GroupApplyService::getInstance()->find($id);
        if (empty($record)) {
            $this->error('记录不存在，请重新操作!');
        }

        $result = GroupApplyService::getInstance()->remove(array('id'=>$id));
        if ($result) {
            $this->success('删除成功!', '');
        }else {
            $this->error('删除失败，请重新再试!');
        }
    }

    /**
     * 列表数据处理
     */
    protected function _index_data_filter(&$data) {
        $size = count($data);
        $cfgStatus = $this->applyStatus();
        $enterprises = EnterpriseService::getInstance()->listEnterprise();

        for ($i=0; $i<$size; $i++) {
            $temp = $data[$i];

            $temp['enterprise'] = "";
            foreach ($enterprises as $val) {
                if ($val == $temp['name']) {
                    $temp['enterprise'] = "已存在";
                }
            }

            $temp['status_txt'] = $cfgStatus[$temp['status']];

            $data[$i] = $temp;
        }

        $this->assign('cfg_status', $cfgStatus);
    }

    /**
     * 体检订单支付状态
     */
    private function applyStatus() {
        return array('1'=>'待审核', '2'=>'已通过', '3'=>'已拒绝');
    }
}

